package com.meng.message.controller.auth;


import com.meng.message.domain.user.MiniUserDetails;
import com.meng.message.domain.vo.ResponseVO;
import com.meng.message.domain.vo.UserInfoVO;
import com.meng.message.domain.vo.UserSaveVO;
import com.meng.message.enums.ErrorEnum;
import com.meng.message.service.auth.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * 用户表 前端控制器
 * @author: Xu Meng
 * @program: minions-cloud
 * @create: 2021-01-14 23:06
 **/
@Api(tags = "用户表")
@Slf4j
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/getUserVoByLoginName")
    public ResponseVO<?> getUserVoByLoginName(@ApiParam(value = "登录用户名",required = true)
                                                          @RequestParam(value = "loginName") String loginName){
        return ResponseVO.result(ErrorEnum.SUCCESS,sysUserService.getUserVoByLoginName(loginName));
    }

    @PostMapping(value = "/saveOrUpdateUser")
    public ResponseVO<?> saveOrUpdateUser(@RequestBody UserSaveVO user) throws Exception {
        boolean flag = sysUserService.saveOrUpdateUser(user);
        return ResponseVO.result(ErrorEnum.SUCCESS,flag);
    }

    @GetMapping(value = "/delUser")
    public ResponseVO<?> delUser(@RequestParam Long userId){
        boolean flag = sysUserService.delUser(userId);
        return ResponseVO.result(ErrorEnum.SUCCESS,flag);
    }

    @GetMapping(value = "/findUserList")
    public ResponseVO<?> findUserList(@RequestParam(required = false, defaultValue = "1", value = "pageNum") int pageNum,
                                      @RequestParam(required = false, defaultValue = "10", value = "pageSize") int pageSize,
                                      @RequestParam(required = false,  value = "search") String search){
        return ResponseVO.result(ErrorEnum.SUCCESS,sysUserService.findUserList(pageNum,pageSize,search));
    }

    @GetMapping("/getUseInfo")
    public ResponseVO<?> getUserInfo(){
        MiniUserDetails user = (MiniUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserInfoVO userInfo = sysUserService.getUserInfoByUserId(user);
        return ResponseVO.result(ErrorEnum.SUCCESS,userInfo);
    }
}

